package cn.zyy.hotel.service.impl;

import cn.zyy.hotel.dao.CheckinMapper;
import cn.zyy.hotel.dao.OrdersMapper;
import cn.zyy.hotel.dao.RoomTypeMapper;
import cn.zyy.hotel.entity.Checkin;
import cn.zyy.hotel.entity.Orders;
import cn.zyy.hotel.entity.RoomType;
import cn.zyy.hotel.service.CheckinService;
import cn.zyy.hotel.vo.CheckinVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CheckinServiceImpl implements CheckinService {

    @Resource
    private CheckinMapper checkinMapper;

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private RoomTypeMapper roomTypeMapper;

    //查询入住列表
    public List<Checkin> selectCheckinList(CheckinVO checkinVO) {
        return checkinMapper.selectCheckinList(checkinVO);
    }

    //添加入住信息
    @Transactional(rollbackFor = RuntimeException.class)
    public int addCheckin(Checkin checkin) {
        //修改入住状态 1-已入住
        checkin.setStatus(1);
        checkin.setCreateDate(new Date()); //创建时间
        //添加入住信息
        int line = checkinMapper.addCheckin(checkin);
        if (line > 0){
            //修改预定订单的状态  改为 3 - 已入住
            Orders orders = new Orders();
            orders.setId(checkin.getOrdersId());
            orders.setStatus(3);
            //调用修改订单方法
            ordersMapper.updateOrders(orders);
            //修改房型表中的已入住数量
            RoomType roomType = roomTypeMapper.selectById(checkin.getRoomTypeId());
            roomType.setLivedNum(roomType.getLivedNum()+1);
            roomTypeMapper.updateRoomType(roomType);
        }
        return line;
    }


}
